//
//  SEGIbeaconDoorManager.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/7/10.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGIbeaconDoorManager.h"
#import "SEGIbeacon.h"

@interface SEGIbeaconDoorManager () <CBPeripheralManagerDelegate>

@property (nonatomic, strong) SEGIbeacon* iBeaconData;

@end

@implementation SEGIbeaconDoorManager
- (instancetype)init
{
    self = [super init];
    if (self) {
        //监听app是否退到后台 退到后台停止发送开门广播
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
    }
    return self;
}

- (void)openDoor
{
    [self iBeaconAdvertising];
}

//ibeacon Tips：1.校时广播时间要从服务器取，没有网络不发送
//      2.点击开门或者退出界面应用时要停止发送校时广播
//      3.发送频率 0.5秒一次，发送两秒后停止

// 发送开门广播
- (void)iBeaconAdvertising
{
    [self.peripheralManager stopAdvertising];
    
    self.iBeaconData = [[SEGIbeacon alloc] init];
    self.iBeaconData.deviceId = self.doorModel.ssid; //SSID
    
    NSUUID* proximityUUID = [[NSUUID alloc] initWithUUIDString:self.iBeaconData.UUID];
    CLBeaconRegion* region = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID
                                                                     major:[self.iBeaconData.major intValue]
                                                                     minor:[self.iBeaconData.minor intValue]
                                                                identifier:@"iBeaconChiang"];
    NSDictionary* beaconPeripheralData = [region peripheralDataWithMeasuredPower:nil];
    [self.peripheralManager startAdvertising:beaconPeripheralData];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.peripheralManager stopAdvertising];
        if (self.iBeaconData) {
            self.iBeaconData = nil;
        }
    });
    [self afterOpenDoorSuccess];
}

// 发送校时广播
- (void)iBeaconTimeAdvertisingWithTime:(NSString*)time
{
    self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
    
    self.iBeaconData = [[SEGIbeacon alloc] init];
    self.iBeaconData.networkTime = time; //取网络时间
    
    NSUUID* proximityUUID = [[NSUUID alloc] initWithUUIDString:self.iBeaconData.timeUUID];
    CLBeaconRegion* region = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID
                                                                     major:[self.iBeaconData.major intValue]
                                                                     minor:[self.iBeaconData.timeMinor intValue]
                                                                identifier:@"iBeaconChiang"];
    NSDictionary* beaconPeripheralData = [region peripheralDataWithMeasuredPower:nil];
    [self.peripheralManager startAdvertising:beaconPeripheralData];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.peripheralManager stopAdvertising];
        if (self.iBeaconData) {
            //            [self.iBeaconData release];
            self.iBeaconData = nil;
        }
    });
}

// 退到后台停止发送开门广播
- (void)appDidEnterBackground
{
    if (self.peripheralManager) { //停止广播
        [self.peripheralManager stopAdvertising];
    }
}

- (void)afterOpenDoorSuccess
{
    //通知代理 做后续操作
//    if (self.delegate && [self.delegate respondsToSelector:@selector(managerOpenDoorSuccessWithShouldShowTips:)]) {
//        [self.delegate managerOpenDoorSuccessWithShouldShowTips:NO];
//    }
    if (self.delegateSubject) {
        [self.delegateSubject sendNext:nil];
        [self.delegateSubject sendCompleted];
    }
}

#pragma mark - CBPeripheralManagerDelegate
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager*)peripheral
{
    switch (peripheral.state) {
            //蓝牙开启且可用
        case CBPeripheralManagerStatePoweredOn:
        {
            break;
        }
        default:
            break;
    }
}
@end
